package vn.com.bookstore.adapter;

import java.util.ArrayList;

import vn.com.bookstore.ObjectFragment;
import vn.com.bookstore.R;
import vn.com.bookstore.fragment.PageBookFragment;
import vn.com.bookstore.model.Book;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class BookViewAdapter extends FragmentStatePagerAdapter{
	Book data;
	Context context;
	ArrayList<Fragment> lstFragment = new ArrayList<Fragment>();
	public BookViewAdapter(Context context,FragmentManager frgMng,Book book) {
		super(frgMng);
		this.context = context;
		this.data = book;
		
		for(int i=0;i<book.pages.size();i++){
			Fragment fragment = new PageBookFragment(data.pages.get(i),data);
			lstFragment.add(fragment);
		}
	}
	
	public ArrayList<Fragment> getListFragment(){
		return lstFragment;
	}
	
	public void setData(Book data){
		this.data = data;
		lstFragment.clear();
		for(int i=0;i<data.pages.size();i++){
			Fragment fragment = new PageBookFragment(data.pages.get(i),data);
			lstFragment.add(fragment);
		}
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return data.pages.size();
	}
	
	@Override
	public Fragment getItem(int position) {
		PageBookFragment fragment = (PageBookFragment)lstFragment.get(position);
		fragment.setData(this.data.pages.get(position), this.data.isComics());
		return fragment;
	}
	
	@Override
    public CharSequence getPageTitle (int position) {
        return context.getString(R.string.page)+" "+data.pages.get(position).page+": "+data.pages.get(position).title;
    }
}
